﻿using MS_Enitity;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MS_UI
{
    public partial class Lotfrm : Form
    {
        public Lotfrm()
        {
            InitializeComponent();
        }




        private void timer1_Tick(object sender, EventArgs e)
        {
            ControlStatusUpdate();

        }


        private void Lotfrm_Load(object sender, EventArgs e)
        {
            timer1.Interval = 100;
            timer1.Enabled = true;
            //BtnStatusUpdate();
            ControlStatusUpdate();
        }

     

        private void Btn_MouseDown(object sender, MouseEventArgs e)
        {
            CommMethods.WritePLCvalue(true, (Button)sender, false);
        }

        private void Btn_MouseUp(object sender, MouseEventArgs e)
        {
            CommMethods.WritePLCvalue(false, (Button)sender, false);
        }

        private void Btn_Click(object sender, EventArgs e)
        {

            string Name = (string)(((Button)sender).Tag);

            if (CommMethods.GetControlValue(Name))
            {
                CommMethods.WritePLCvalue(false, (Button)sender, true);
            }
            else
            {
                CommMethods.WritePLCvalue(true, (Button)sender, true);
            }
        }

        private void Lbl_Click(object sender, EventArgs e)
        {
            CommMethods.WriteTxtValue(sender);
        }

        private void BtnStatusUpdate()
        {


            foreach (Control item in Controls)
            {

                //if (item is Button)
                //{
                //    if ((string)((Button)item).Tag != null && (string)((Button)item).Tag != "自动按钮/7")
                //    {
                //        bool BtnValue = CommMethods.GetControlValue((string)(((Button)item).Tag));
                //        if (BtnValue)
                //        {
                //            ((Button)item).BackColor = Color.Red;
                //            ((Button)item).Text = "关闭";
                //        }
                //        else
                //        {
                //            ((Button)item).BackColor = Color.FromArgb(224, 224, 224);
                //            ((Button)item).Text = "启用";
                //        }
                //    }
                //}
            }
        }




        private void ControlStatusUpdate()
        {
            LotID_1.Text = CommMethods.SLot_ID1;
            LotID_2.Text = CommMethods.SLot_ID2;
            LotID_3.Text = CommMethods.SLot_ID3;
            LotID_4.Text = CommMethods.SLot_ID4;
            LotID_Run.Text = CommMethods.SLotrun_ID4;
            foreach (Control item in Controls)
            {
               

                if (item is Label && (string)((Label)item).Tag != null)
                {

                    string VarName = (string)(((Label)item).Tag);
                    Int16_Variable var = CommMethods.Int16_Variable_List.Find(delegate (Int16_Variable v) { return v.VarName == VarName; });
                    int index = CommMethods.Int16_Variable_List.IndexOf(var);
                    index += 1;
                    //Int16_Variable var2 = CommMethods.Int16_Variable_List[index];
                    if (var != null)
                    {
                        if (var.IsInt32 == "Y")
                        {
                            Int16_Variable var2 = CommMethods.Int16_Variable_List[index];
                            int[] values = new int[2];
                            values[0] = Convert.ToInt32(CommMethods.CurrentValue[VarName]);
                            values[1] = Convert.ToInt32(CommMethods.CurrentValue[var2.VarName]);
                            int intvalue = (values[0] & 0xffff) | (values[1] << 16);
                            float floatvalue = ((float)intvalue) / Convert.ToSingle(Math.Pow(10, Convert.ToInt32(var.PointNO)));
                            ((Label)item).Text = Convert.ToString(floatvalue);
                        }

                        if (var.IsInt32 == "N")
                        {

                            ((Label)item).Text = Convert.ToString(float.Parse(CommMethods.CurrentValue[VarName]) / Convert.ToSingle(Math.Pow(10, Convert.ToInt32(var.PointNO))));
                        }
                    }

                }







            }
        }

        private void Lot2_NO_Click(object sender, EventArgs e)
        {

        }

   
    }
}
